package demo2;

import java.util.LinkedList;
import java.util.Queue;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: LianBao
 * Date: 2024-03-31
 * Time: 15:54
 */

class MyQueue {

    static class Node {
        public int val;
        public Node prev;
        public Node next;

        public Node(int val) {
            this.val = val;
        }
    }

    public Node head;
    public Node last;

    //尾插
    public void offer(int val) {
        Node node = new Node(val);
        if (head == null) {
            head = last = node;
        } else {
            last.next = node;
            node.prev = last;
            last = node;
        }
    }

    //头删
    public int poll() {
        try {
            if (head == null) {
                throw new NullPointerException();
            }
        } catch (NullPointerException e) {
            System.out.println();
            e.printStackTrace();
        }

        int ret = head.val;
        if (head.next == null) {
            head = null;
        } else {
            head = head.next;
            head.prev = null;
        }
        return ret;
    }

    public int peek() {
        try {
            if (head == null) {
                throw new NullPointerException();
            }
        } catch (NullPointerException e) {
            System.out.println();
            e.printStackTrace();
        }

        return head.val;
    }

    public boolean empty() {
        return head == null;
    }
}


public class queue {
    public static void main(String[] args) {
        Queue<Integer> queue = new LinkedList<>();

        queue.offer(1);//offer尾插
        queue.poll();//poll头删
        int ret = queue.peek();//peek
        //.....
        queue.add(1);//      =>offer
        queue.remove();//    =>pool
        queue.element();//   =>peek
        //add与offer的区别?
        queue.isEmpty();
    }
}
